﻿using System;
using M.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RpcClient;
using static M.Core.ConsulExtensions;

namespace M.ServiceApi2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var thriftServiceType = typeof(InnerApi);

            services.AddConsul();
            //注册WebApi服务
            services.AddApiService(thriftServiceType);
            //注册Thrift服务
            services.AddRpcCallService(thriftServiceType);
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            ServiceOptions serviceEntity = new ServiceOptions()
            {
                ConsulIP = Configuration["Consul:IP"] ?? "192.168.3.165",
                ConsulPort = Configuration["Consul:Port"] != null ? Convert.ToInt32(Configuration["Consul:Port"]) : 8500,
                IP = Configuration["Consul:IIS.IP"] != null ? Configuration["Consul:IIS.IP"] : null,
                Port = Configuration["Consul:IIS.Port"] != null ? Convert.ToInt32(Configuration["Consul:IIS.Port"]) : 0,
                Version = Configuration["Version"] != null ? (float)Convert.ToDouble(Configuration["Version"]) : 1.0f,
                ThriftPort = Convert.ToInt32(Configuration["Consul:Thrift.Port"])
            };
            app.UseConsul(serviceEntity);
            app.UseCallService();

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
